<template>
	<scroll-view scroll-with-animation scroll-y="true" :scrollTop="rightScrollTop" :scroll-into-view="scrollIndex"
		@scroll="goodsScroll" class="goods-list-box">
		<view class="good-list">
			<block v-for="(goodsCategory, i) in listData" :key="goodsCategory.category_no">
				<view class="g-list" :id="'goods_index_' + i" :key="goodsCategory.id">
					<view class="list-title">
						<view class="list-title-val">{{ goodsCategory.category_name }}</view>
					</view>
					<block v-if="goodsCategory.products && goodsCategory.products.length > 0">
						<block v-for="(goodSProduct, goods_index) in goodsCategory.products" :key="goodSProduct.id">
							<view class="good-li" :class="{ 'is-c-out': goodSProduct.is_c_out }">
								<view class="good-img" v-show="!isCart"
									@click.stop="showDetails(goodSProduct, goods_index)">
									<u--image :lazy-load="true" :showLoading="true" :src="goodSProduct.image" width="144rpx"
										height="144rpx" :fade="true" duration="50" radius="8">
										<template v-slot:loading>
											<u-loading-icon color="#ff6050"></u-loading-icon>
										</template>
									</u--image>
									<view class="sell-out" v-if="goodSProduct.is_c_out">售罄</view>
								</view>
								<view class="flex-item">
									<view class="flex good-name" @click.stop="showDetails(goodSProduct, goods_index)">
										<view class="product-name">{{ goodSProduct.product_name }}</view>
										<view class='btn-spec' v-if="goodSProduct.product_kind == 1">
											<view class="goods-mark">多规格</view>
										</view>
										<view class='btn-spec' v-if="goodSProduct.product_kind == 2">
											<view class="goods-mark">可选套餐</view>
										</view>
										<!-- <view class='btn-spec' v-if="goodSProduct.product_kind == 3">
											<view class="goods-mark">可选要求</view>
										</view>
										<view class='btn-spec' v-if="goodSProduct.product_kind == 4">
											<view class="goods-mark">可选套餐</view>
										</view> -->
									</view>
									<view class="good-bottom">
										<view class="good-price" @click.stop="showDetails(goodSProduct, goods_index)">
											<view class="red">
												<view class="goods-price-sale"
													v-if="goodSProduct.unit && goodSProduct.unit.length > 0">
													￥<text>{{ goodSProduct.unit[0].price_level || 0 }}</text>
												</view>
												<!-- <view class="special-offer" v-if="goodSProduct.te && goodSProduct.te == 1">
													特价</view> -->
											</view>
											<view class="old-price" v-if="goodSProduct.tag != 0">
												￥{{ goodSProduct.current_price }}
											</view>
											<view class="spec-key" v-if="isCart && goodSProduct.product_kind == 1">
												{{ goodSProduct.food_property }}
											</view>
										</view>
										<cartControl @update-food-count="updateFoodCount"></cartControl>
									</view>
								</view>
							</view>
						</block>
					</block>
				</view>
			</block>
			<view class="text-c no-more-info">没有更多了~~</view>
		</view>
		<!-- 商品详情弹窗 -->
		<goodsInfo ref="goodsInfoRef" :currentGood="currentGood" @detailsAddFn="detailsAddFn" />
		<!-- 选择规格弹窗 -->
		<u-popup :safeAreaInsetBottom="true" :show="showGoodSpec" :round="20" mode="bottom" :closeable="true"
			@close="closeGoodsCheck()" @touchmove.stop>
			<view class="flex current-good-spec-box" v-if="currentGood && Object.keys(currentGood).length > 0">
				<view style="display: none;">{{ JSON.stringify(currentGood) }}</view>
				<view class="flex title-box">
					<view class="left-img-box">
						<image class="img-hook" :src="currentGood.image" />
					</view>
					<view class="flex info-content">
						<view class="goods-name">{{ currentGood.product_name }}</view>
						<view class="select-text">已选:{{ currentGoodSelectStr }}</view>
					</view>
				</view>
				<view class="goods-number">
					<view class="font-style">数量</view>
					<cartControl></cartControl>
				</view>
				<view class="good-spec-box">
					<view class="group-box" v-show="currentGood.unit.length > 0">
						<view class="group-title">规格</view>
						<view class="group-specs">
							<view v-for="(unitItem, g) in currentGood.unit" :key="g.toString()" @click="selectSpec(g)"
								:class="{ spec: true, select: unitItem.select }">
								<text>{{ unitItem.unit_name }}</text>
								<text v-if="unitItem.price > 0" style="margin-left: 8rpx">
									{{ unitItem.price > 0 ? "￥" + unitItem.price : "" }}
								</text>
							</view>
						</view>
					</view>
					<!-- <view class="group-box" v-show="currentGood.practice.length > 0">
						<view class="group-title">做法</view>
						<view class="group-specs">
							<view v-for="(unitItem, g) in currentGood.practice" :key="g.toString()"
								@click="selectPracticec(g)" :class="{ spec: true, select: unitItem.select }">
								<text>{{ unitItem.unit_name }}</text>
								<text v-if="unitItem.price > 0" style="margin-left: 8rpx">
									{{ unitItem.price > 0 ? "￥" + unitItem.price : "" }}
								</text>
							</view>
						</view>
					</view>
					<view class="group-box" v-show="currentGood.taste.length > 0">
						<view class="group-title">口味</view>
						<view class="group-specs">
							<view v-for="(unitItem, g) in currentGood.taste" :key="g.toString()" @click="selectTaste(g)"
								:class="{ spec: true, select: unitItem.select }">
								<text>{{ unitItem.unit_name }}</text>
								<text v-if="unitItem.price > 0" style="margin-left: 8rpx">
									{{ unitItem.price > 0 ? "￥" + unitItem.price : "" }}
								</text>
							</view>
						</view>
					</view>
					<view class="group-box">
						<view class="group-title">加价小食</view>
						<view class="group-specs">
							<view v-for="(unitItem, g) in currentGood.combos" :key="g.toString()" @click="selectCombosFn(g)"
								class="spec flex" :class="{ select: unitItem.select }">
								<text>{{ unitItem.unit_name }}{{ unitItem.price > 0 ? "￥" + unitItem.price : ""
								}}</text>
								<view v-show="unitItem.sumNum > 0" style="margin-left: 8rpx">
									x{{ unitItem.sumNum }}
								</view>
								<view v-show="unitItem.sumNum > 0" style="margin-left: 8rpx" @click.stop="clearNum(g)">
									<u-icon name="close-circle-fill" size="36"></u-icon>
								</view>
							</view>
						</view>
					</view> -->
				</view>
				<!-- <view class="flex good-spec-bottom">
					<view class="good-price" style="font-size: 36rpx">
						<view class="red">
							￥ <text>{{ goodsPriceCount + morePriceVal }}</text>
							<text style="font-size: 28rpx; color: #ccc">/{{ specVal }}</text>
							<text class="sub" style="font-size: 26rpx">
								（已加价￥ {{ morePriceVal }}）
							</text>
						</view>
					</view>
					<view>
						<u-button text="加入购物车" shape="circle" color="#FF6050" iconColor="#f9ae3d" @click="addToCart">
						</u-button>
					</view>
				</view> -->
			</view>
		</u-popup>
		<u-popup :safeAreaInsetBottom="true" :show="showGoodCompose" :round="20" mode="bottom" :closeable="true"
			@close="composeClose()" @touchmove.stop>
			<view class="current-good-spec-box" v-if="currentGood && Object.keys(currentGood).length > 0">
				<view class="flex position-rel title-box">
					<view class="left-img-box">
						<image class="img-hook" :src="currentGood.line_image_url" />
					</view>
					<view class="flex position-rel info-content">
						<view>{{ currentGood.product_name }}</view>
					</view>
					<view class="text-c position-abs reset-btn" @click="resetSpec()">
						重选
					</view>
				</view>
				<view class="goods-number">
					<view class="font-style">数量</view>
					<view style="display: flex;flex-direction: row">
						<u-icon name="minus-circle" color="#FF6050" size="48"></u-icon>
						<view style="width: 80rpx;text-align: center;font-size: 36rpx;">{{ currentGood.qty }}
						</view>
						<u-icon name="plus-circle-fill" color="#FF6050" size="48"></u-icon>
					</view>
				</view>
				<scroll-view class="good-spec-box" :scroll-y="true">
					<view class="group-box" v-for="(combos_item, combos_item_index) in currentGood.combos"
						:key="combos_item_index.toString()">
						<view class="flex group-title">
							{{ combos_item.compose_name }}
							<view class="sub-combos">
								({{ combos_item.select_qty }} - {{ combos_item.select_number }})
							</view>
						</view>
						<view class="group-specs">
							<view class="position-rel condiment_box spec" :class="{ select: combos_item_dtl.select }"
								v-for="(combos_item_dtl, g) in combos_item.dtl" :key="g.toString()"
								@click="selectCombosDtl(g, combos_item_index)">
								<text> {{ combos_item_dtl.product_name }} </text>
								<text v-if="combos_item_dtl.select_number > 0" class="position-abs check-num-count">
									{{ combos_item_dtl.select_number }}
								</text>
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="flex good-spec-bottom">
					<view class="good-price" style="font-size: 34upx">
						<view class="red">
							￥50
						</view>
					</view>
					<view>
						<u-button shape="circle" color="#ff6050" @click="addToCart">
							<view
								style="display: flex;flex-direction: column;justify-content: center;align-items: center;height: 100%">
								<view style="height: 35rpx;flex: 1">加入购物车</view>
								<view style="font-size: 24rpx;color: yellow" v-show="showNeedSelect">(尚有应选品)</view>
							</view>
						</u-button>
					</view>
				</view>
			</view>
		</u-popup>
	</scroll-view>
</template>

<script>
import goodsInfo from './goodsInfo.vue';
import cartControl from '@/components/cartControl/cartControl.vue';

import _ from "lodash";

import { goodsData } from '@/api/goodsData3.js'

export default {
	name: "goodsList",
	props: {
		topHeight: {
			type: Number,
			default: () => {
				return 0
			}
		},
		showCls: {
			type: Boolean,
			default: () => {
				return true;
			},
		},
		canSelect: {
			type: Boolean,
			default: () => {
				return true;
			},
		},
		isCart: {
			type: Boolean,
			default: () => {
				return false;
			},
		},
		// listData: {
		// 	type: Array,
		// 	default:() => {
		// 		return [];
		// 	},
		// }
	},
	data() {
		return {
			scrollIndex: '',
			rightScrollTop: 0,
			showGoodCompose: false,
			listData: goodsData,
			specVal: 0,
			initGood: {},
			currentGood: undefined,
			showGoodSpec: false,
		};
	},
	components: {
		goodsInfo,
		cartControl
	},
	mounted() { },
	computed: {
		updateFoodCount(food, isAdd, event) {
			if (!event._constructed) {
				return;
			}

			if (isAdd) {
				// 增加
				if (!food.count) {
					// 第一次
					// console.log('updateFoodCount() 第一次')
					// 新增count属性
					// food.count = 1  // 没有监视-->没有数据绑定, 界面不会更新
					Vue.set(food, "count", 1);
				} else {
					food.count++;
				}
				// 通知shopcart组件对象启动一个小球的显示动画
				this.$refs.shopcart.drop(event.target)
			} else {
				// 减少
				if (food.count) {
					food.count--;
				}
			}
		},
		currentGoodSelectStr() {
			if (!this.currentGood) return '';
			let str = ""
			let good = _.cloneDeepWith(this.currentGood)
			_.each(good.units, item => {
				if (item.select) {
					str += item.unit_name
				}
			})
			_.each(good.practice, item => {
				if (item.select) {
					str += '+' + item.unit_name
				}
			})
			_.each(good.taste, item => {
				if (item.select) {
					str += '+' + item.unit_name
				}
			})
			return str
		},
		goodsPriceCount() {
			if (!this.currentGood) return 0;
			let rs = this.currentGood.units.filter((item) => item.select);
			if (rs.length > 0) {
				this.getSpec(rs);
				return rs[0].currentGoodPrice;
			} else {
				this.getSpec();
				return 0
			}
		},
		morePriceVal() {
			if (!this.currentGood) return 0;
			let rs = this.currentGood.combos.filter((item) => item.sumNum > 0);
			if (rs.length > 0) {
				let oNum = 0;
				rs.map((item) => {
					oNum = oNum + item.price * item.sumNum;
				})
				return oNum
			} else {
				return 0
			}
		},
		// 当前套餐是否选中应选品
		showNeedSelect() {
			if (!this.currentGood) return false;
			const combos = this.currentGood.combos
			let noSelectAll = false
			_.each(combos, item => {
				if (item.select_number != item.select_qty) {
					noSelectAll = true
				}
			})
			return noSelectAll
		}
	},
	methods: {
		async changeScrollHiehgt(index) {
			this.scrollIndex = 'goods_index_' + index;
		},
		goodsScroll(e) {
			var that = this;
			that.$nextTick(async () => {
				const query = uni.createSelectorQuery().in(that);
				query.selectAll('.list-title').boundingClientRect(data => {
					for (let index = 0; index < data.length; index++) {
						if (data[index].top == this.topHeight) {
							that.$emit('handleGoodsIndex', index);
						}
					}
				}).exec();
			})
		},
		// 关闭单品选择弹窗
		closeGoodsCheck() {
			this.showGoodSpec = false;
			this.$nextTick(() => {
				this.currentGood = undefined;
			})
		},
		getSpec(rs) {
			this.specVal = rs ? rs[0].unit_name : '';
		},
		// 选择规格
		specSelect(good, index) {
			this.currentGood = undefined;
			let oGoods = good;
			this.currentGood = _.cloneDeepWith(oGoods);
			if (good.product_kind == 1) {
				this.showGoodSpec = true;
			} else if (good.product_kind == 4) {
				this.showGoodCompose = true;
			}
		},
		// 详情添加
		detailsAddFn() {
			if (this.currentGood.product_kind == 1) {
				this.showGoodSpec = true;
			} else if (this.currentGood.product_kind == 4) {
				this.showGoodCompose = true;
			}
		},
		// 展示产品详情页面
		showDetails(goods) {
			this.currentGood = _.cloneDeepWith(goods);
			this.initGood = _.cloneDeepWith(goods);
			if (this.$refs.goodsInfoRef) {
				this.$refs.goodsInfoRef.showModal = true;
			}
		},
		// 仅计算可以直接加入购物车的产品
		getProductQty(good) {
			let qty = 0
			// if (this.isCart) {
			// 	return good.qty
			// }
			_.each(this.cart_info, item => {
				if (good.product_no == item.product_no) {
					qty = item.qty
				}
			})
			return qty
		},


		// 改变数量
		changeQty(e) {
			this.currentGood.qty = e.value
			this.calcProductPrice()
		},

		/**
		 * 列表中进行购物车商品追加
		 * @param tyle
		 * @param good_tmp
		 */
		changeCart(tyle, product, index) {
			const cart_info = _.cloneDeepWith(this.cart_info)
			// 购物车中的加减
			if (this.isCart) {
				switch (tyle) {
					case '-':
						cart_info[index].qty -= 1
						if (cart_info[index].qty <= 0) {
							// 删除这一条
							cart_info.splice(index, 1)
							break
						}
						break;
					case '+':
						cart_info[index].qty += 1
						break;
					default:
						break;
				}
				// 进行入购物车
				return false
			}

			let unit = product.units
			if (unit.length <= 0) {
				// 进行错误提示
				uni.showToast({
					title: "产品规格设置有误",
					icon: "error"
				})
				return false
			}

			// 代表单规格，直接加入购物车
			if (product.is_select) {
				unit = unit[0]
				// 查询购物车中是否有这个产品
				let hasProduct = false
				_.each(cart_info, (item, index) => {
					if (item.product_no == product.product_no && item.unit_product_no == unit
						.unit_product_no) {
						tyle == "+" ? item.qty += 1 : item.qty -= 1
						hasProduct = true
					}
				})
				// 购物车进行产品追加
				if (!hasProduct) {
					let condimentArr = []
					_.map(product.condiment_group, (condiment_group_item) => {
						_.map(condiment_group_item.condiment, (condiment) => {
							condimentArr.push({
								condiment_name: condiment.condiment_name,
								condiment_no: condiment.condiment_no,
								inv_qty: condiment.inv_qty,
								price: condiment.price
							})
						})
					})
					cart_info.push({
						"product_no": product.product_no,
						// "product_name": product.product_name,
						"alt_name1": product.alt_name1,
						"unit_product_no": unit.unit_product_no,
						"unit_name": unit.unit_name,
						"price": unit.price,
						"current_price": unit.current_price,
						"vip_price": unit.vip_price,
						"tag": unit.tag,
						"qty": 1,
						"box_price": unit.box_price,
						"condiment": condimentArr,
						"combos": product.combos,
						"non_product": true,
						"product_kind": product.product_kind,
						"bargain_no": product.bargain_no,
						"product_set_calc": product.product_set_calc
					})
				}
			}
		},

		// 打开套餐弹窗
		composeSelect(good) {
			// 购物车中查询上一次这个产品最后选择的属性
			const currentGood = _.cloneDeepWith(good)

			const lastIndex = _.findLastIndex(this.cart_info, item => {
				return item.product_no == currentGood.product_no && item.product_kind == 2 // 套餐头
			})

			const selectCartInfo = this.cart_info[lastIndex]
			currentGood.qty = 1 // 处理默认选择的数量
			// 选择的套餐子菜编号和数量
			const cartComposeNoArr = []
			if (lastIndex > -1) {
				currentGood.qty = selectCartInfo.qty
				_.each(selectCartInfo.combos, item => {
					cartComposeNoArr[item.compose_no + '_' + item.product_no] = item.select_number
				})
			}
			// 选择的调味编号和数量
			const cartCondimentNoArr = []
			if (lastIndex > -1) {
				currentGood.qty = selectCartInfo.qty
				_.each(selectCartInfo.condiment, item => {
					cartCondimentNoArr[item.condiment_no] = item.qty
				})
			}

			// 规格默认选中处理
			_.each(currentGood.units, item => {
				item.select = false
				if (lastIndex > -1 && item.unit_product_no == selectCartInfo.unit_product_no) {
					item.select = true
				}
			})
			// 购物车中没有，默认选中第一个规格
			if (lastIndex == -1) {
				currentGood.units[0].select = true
			}


			// 默认子菜选中处理
			_.each(currentGood.combos, item => {
				// 已选
				item.select_number = 0
				// 循环子菜
				_.each(item.dtl, combos_dtl_item => {
					let hasCombosDtl = false
					combos_dtl_item.select = false
					combos_dtl_item.select_number = 0


					// 购物车里面是否有

					if (lastIndex > -1 && cartComposeNoArr[item.compose_no + '_' + combos_dtl_item
						.product_no]) {
						combos_dtl_item.select = true
						combos_dtl_item.select_number = cartComposeNoArr[item.compose_no + '_' +
							combos_dtl_item.product_no]
						hasCombosDtl = true
					}

					// 默认选中
					if (!hasCombosDtl && combos_dtl_item.is_default) {
						combos_dtl_item.select = true
						combos_dtl_item.select_number = 1
					}

					item.select_number += combos_dtl_item.select_number
				})
				// 默认选中
				// if(!hasCombosDtl){
				//   item.condiment[0].select = true
				//   item.condiment[0].select_number = 1
				// }
			})

			// 默认调味选中处理
			_.each(currentGood.condiment_group, item => {
				let hasCondiment = false
				_.each(item.condiment, condiment_item => {
					condiment_item.select = false
					condiment_item.select_number = 0
					if (lastIndex > -1 && cartCondimentNoArr[condiment_item.condiment_no]) {
						condiment_item.select = true
						condiment_item.select_number = cartCondimentNoArr[condiment_item.condiment_no]
						hasCondiment = true
						return true
					}
					if (condiment_item.line_default) {
						condiment_item.select = true
						condiment_item.select_number = 1
						hasCondiment = true
					}
				})
				// 默认选中
				if (!hasCondiment) {
					item.condiment[0].select = true
					item.condiment[0].select_number = 1
				}
			})

			// 价格
			currentGood.currentGoodPrice = 0
			// 规格相关
			currentGood.unit_name = ''
			// 加价相关
			currentGood.currentGoodPlusPrice = 0
			if (lastIndex > -1) {
				currentGood.currentGoodPrice = selectCartInfo.total
				currentGood.unit_name = selectCartInfo.unit_name
				currentGood.currentGoodPlusPrice = selectCartInfo.condiment_price
			}
			this.initGood = _.cloneDeepWith(currentGood)
			this.calcProductPrice()

		},

		// 当前规格加入购物车
		addToCart() {
			// 尚有应选品
			if (this.showNeedSelect) {
				return false
			}
			const currentGood = _.cloneDeepWith(this.currentGood)
			// 规格
			for (var i in currentGood.units) {
				let item = currentGood.units[i]
				if (!item.select) {
					currentGood.units.splice(i, 1)
				}
			}
			const selectUnit = currentGood.units[0]
			const unit_product_no = currentGood.units[0].unit_product_no
			// 调味
			const condimentArr = []
			// 当前选中的所有调味编号
			const condimentNoArr = []
			_.each(currentGood.condiment_group, item => {
				_.each(item.condiment, condiment_item => {
					if (condiment_item.select) {
						condimentNoArr.push(condiment_item.condiment_no)
						condimentArr.push({
							condiment_no: condiment_item.condiment_no,
							condiment_name: condiment_item.condiment_name,
							price: condiment_item.price,
							qty: condiment_item.select_number,
							inv_qty: condiment_item.inv_qty
						})
					}
				})
			})
			// 当前选中的所有套餐编号
			const combosArr = []
			const combosNoArr = []
			_.each(currentGood.combos, item => {
				_.each(item.dtl, dtl_item => {
					if (dtl_item.select) {
						combosNoArr.push(dtl_item.unit_product_no)
						combosArr.push({
							product_no: dtl_item.product_no,
							// product_name: dtl_item.product_name,
							unit_product_no: dtl_item.unit_product_no,
							unit_name: dtl_item.unit_name,
							current_price: dtl_item.current_price,
							qty: _.toNumber(dtl_item.select_number) * _.toNumber(dtl_item
								.qty), // 选择的份数*每一份包含的数量
							select_number: dtl_item.select_number, // 选择项目的数量
							compose_no: item.compose_no
						})
					}
				})
			})


			const cart_info = _.cloneDeepWith(this.cart_info)

			// 查询当前是否有相同的产品，相同的规格，相同的调味
			const cartIndex = _.findLastIndex(cart_info, item => {
				// 购物车当前产品的调味
				let itemCondimentNoArr = []
				_.each(item.condiment, condiment_item => {
					itemCondimentNoArr.push(condiment_item.condiment_no)
				})

				// 购物车当前选中产品的套餐子菜
				let itemCombosNoArr = []
				_.each(item.combos, item => {
					itemCombosNoArr.push(item.unit_product_no)
				})
				// 返回套餐相同，规格相同，套餐相同的 购物车
				return item.product_no == currentGood.product_no && item.unit_product_no == unit_product_no &&
					_.sortedUniq(itemCondimentNoArr).join(',') == _.sortedUniq(condimentNoArr).join(',') &&
					_.sortedUniq(itemCombosNoArr).join(',') == _.sortedUniq(combosNoArr).join(',')
			})
			// 代表购物车找到了相同的规格产品，进行数量追加
			if (cartIndex > -1) {
				const _selectCartInfo = cart_info[cartIndex] // 当前购物车需要操作的产品
				// 产品数量
				_selectCartInfo.qty = currentGood.qty
				return
			}
			cart_info.push({
				"product_no": currentGood.product_no,
				// "product_name": currentGood.product_name,
				"alt_name1": currentGood.alt_name1,
				"unit_product_no": selectUnit.unit_product_no,
				"unit_name": selectUnit.unit_name,
				"price": selectUnit.price,
				"current_price": selectUnit.current_price,
				"vip_price": selectUnit.vip_price,
				"tag": selectUnit.tag,
				"qty": currentGood.qty,
				"box_price": selectUnit.box_price,
				"condiment": condimentArr,
				"combos": combosArr,
				"non_product": true,
				"product_kind": currentGood.product_kind,
				"bargain_no": selectUnit.bargain_no,
				"bargain_price": selectUnit.bargain_price,
				"product_set_calc": currentGood.product_set_calc
			})

		},
		resetSpec() {
			// 拷贝商品
			const oCurrentGood = _.cloneDeepWith(this.initGood);
			this.currentGood = oCurrentGood;
		},
		// 计算每一个分类的高度
		getRect(realBottom = 0) {
			return new Promise((resolve, reject) => {
				let hList = [];
				hList.push(realBottom);
				const query = uni.createSelectorQuery().in(this).selectAll(".g-list");
				let gPos = realBottom;
				query.boundingClientRect((data) => {
					data.forEach((o, i) => {
						// if (i === 0) {
						//     gPos = realBottom;
						// } else {
						gPos += o.height;
						// }
						hList.push(gPos);
					});
					resolve(hList);
				}).exec();
			});
		},
		// 关闭套餐弹窗
		composeClose() {
			this.showGoodCompose = false;
			this.currentGood = undefined;
			this.initGood = undefined;
		},
		// 选择规格
		selectSpec(index) {
			let oCurrentGood = _.cloneDeepWith(this.currentGood);
			if (oCurrentGood.units[index].select) {
				oCurrentGood.units[index].select = false;
			} else {
				_.each(oCurrentGood.units, item => {
					item.select = false
				})
				oCurrentGood.units[index].select = true;
			}

			this.currentGood = oCurrentGood;
		},
		// 选择做法
		selectPracticec(index) {
			let oCurrentGood = _.cloneDeepWith(this.currentGood);
			if (oCurrentGood.practice[index].select) {
				oCurrentGood.practice[index].select = false;
			} else {
				_.each(oCurrentGood.practice, item => {
					item.select = false
				})
				oCurrentGood.practice[index].select = true;
			}

			this.currentGood = oCurrentGood;
		},
		// 选择口味
		selectTaste(index) {
			let oCurrentGood = _.cloneDeepWith(this.currentGood);
			if (oCurrentGood.taste[index].select) {
				oCurrentGood.taste[index].select = false;
			} else {
				_.each(oCurrentGood.taste, item => {
					item.select = false
				})
				oCurrentGood.taste[index].select = true;
			}

			this.currentGood = oCurrentGood;
		},
		// 选择加价小食
		selectCombosFn(index) {
			let oCurrentGood = _.cloneDeepWith(this.currentGood);
			if (oCurrentGood.combos[index].select) {
				oCurrentGood.combos[index].sumNum++;
			} else {
				_.each(oCurrentGood.combos, item => {
					item.select = false
				})
				oCurrentGood.combos[index].select = true;
				if (oCurrentGood.combos[index].sumNum === 0) {
					oCurrentGood.combos[index].sumNum = 1;
				}
			}

			this.currentGood = oCurrentGood;
		},
		// 清除小食
		clearNum(index) {
			let oCurrentGood = _.cloneDeepWith(this.currentGood);
			oCurrentGood.combos[index].sumNum = 0

			this.currentGood = oCurrentGood;
		},

		// 选择调味
		selectCondiment(condimentIndex, condimentGroupIndex) {
			let condimentGroup = this.currentGood.condiment_group[condimentGroupIndex]
			let condiment = condimentGroup.condiment
			// 单选还是多选
			if (condimentGroup.select_type == 'single') {
				_.each(condiment, item => {
					item.select = false
					item.select_number = 0
				})
				condiment[condimentIndex].select = true
				condiment[condimentIndex].select_number = 1
			} else {
				// 多选，必须选择一个
				if (condiment[condimentIndex].price > 0) {
					condiment[condimentIndex].select_number += 1
					condiment[condimentIndex].select = true
					this.calcProductPrice()
					return
				}
				let selectNum = 0
				_.each(condiment, item => {
					if (item.select) selectNum += 1
				})
				condiment[condimentIndex].select_number = 0

				if (selectNum > 1) {
					condiment[condimentIndex].select = !condiment[condimentIndex].select
					condiment[condimentIndex].select_number = condiment[condimentIndex].select_number > 0 ? 0 : 1
				} else {
					condiment[condimentIndex].select_number = 1
					condiment[condimentIndex].select = true
				}

			}
			this.calcProductPrice()
		},

		// 计算当前规格价格
		calcProductPrice() {
			const order_info = this.order_info
			// 获取当前产品的计算价格
			let currentGood = _.cloneDeepWith(this.currentGood)
			// 规格
			for (var i in currentGood.units) {
				let item = currentGood.units[i]
				if (!item.select) {
					currentGood.units.splice(i, 1)
				}
			}
			// 调味
			const condiment = []
			_.each(currentGood.condiment_group, item => {
				_.each(item.condiment, condiment_item => {
					if (condiment_item.select) {
						condiment.push({
							condiment_no: condiment_item.condiment_no,
							price: condiment_item.price,
							qty: condiment_item.select_number
						})
					}
				})
			})
			currentGood.condiment = condiment
			// 套餐相关,计算选中的价格
			const combos = []
			_.each(currentGood.combos, item => {
				_.each(item.dtl, dtl_item => {
					if (dtl_item.select) {
						combos.push({
							product_no: dtl_item.product_no,
							// product_name: dtl_item.product_name,
							unit_product_no: dtl_item.unit_product_no,
							unit_name: dtl_item.unit_name,
							current_price: dtl_item.current_price,
							select_number: dtl_item.select_number, // 选择了几份
							qty: dtl_item.qty // 每一份有多少个
						})
					}
				})
			})
			currentGood.combos = combos

			let currentGoodPrice = 0
			let unit_name = ''
			let currentGoodPlusPrice = 0
			// 计算规格的价格

			_.each(currentGood.units, item => {
				if (currentGood.product_kind == 1 || (currentGood.product_kind == 2 && currentGood
					.product_set_calc == 1)) {
					// 原价
					if (currentGood.tag == 0) {
						currentGoodPrice += item.current_price
					}
					// 特价
					if (currentGood.tag == 1) {
						currentGoodPrice += item.bargain_price
					}
					// 会员价
					if (currentGood.tag == 4) {
						currentGoodPrice += item.vip_price
					}
				}
				unit_name = item.unit_name
			})
			// 计算调味的价格
			_.each(currentGood.condiment, item => {
				currentGoodPrice += (item.price * item.qty)
				currentGoodPlusPrice += (item.price * item.qty)
			})


			// 计算套餐子菜的价格
			_.each(currentGood.combos, item => {
				// 固定价格
				currentGoodPrice += (_.toNumber(item.current_price) * _.toNumber(item.select_number) * _
					.toNumber(item.qty))
				// if (currentGood.product_set_calc==1){
				// }
				// 子菜合计价格


			})

			// 价格
			this.currentGood.currentGoodPrice = parseFloat(currentGoodPrice * currentGood.qty).toFixed(2)
			// 规格
			this.currentGood.unit_name = unit_name
			// 加价
			this.currentGood.currentGoodPlusPrice = parseFloat(currentGoodPlusPrice * currentGood.qty).toFixed(2)
		},

		// 选择套餐
		selectCombosDtl(combosDtlIndex, combosIndex) {
			let combosItem = this.currentGood.combos[combosIndex]
			let combosDtl = combosItem.dtl
			if (combosItem.select_qty <= combosItem.select_number) {
				return
			}
			combosDtl[combosDtlIndex].select = true
			if (!combosDtl[combosDtlIndex].select_number) {
				combosDtl[combosDtlIndex].select_number = 0
			}
			combosDtl[combosDtlIndex].select_number += 1
			combosItem.select_number += 1
			this.calcProductPrice()
		},

		// 计算选中规格的价格
		getSelectProductPrice() {
			// 获取当前产品的计算价格
			let currentGood = _.cloneDeepWith(this.currentGood)
			// 规格
			for (var i in currentGood.units) {
				let item = currentGood.units[i]
				if (!item.select) {
					currentGood.units.splice(i, 1)
				}
			}
			// 调味
			const condiment = []
			_.each(currentGood.condiment_group, item => {
				_.each(item.condiment, condiment_item => {
					if (condiment_item.select) {
						condiment.push({
							condiment_no: condiment_item.condiment_no,
							price: condiment_item.price,
							qty: condiment_item.select_number
						})
					}
				})
			})
			currentGood.condiment = condiment
			// 套餐相关
			const combos = []
			_.each(currentGood.combos, item => {
				_.each(item.dtl, dtl_item => {
					if (dtl_item.select) {
						combos.push({
							product_no: dtl_item.product_no,
							// product_name: dtl_item.product_name,
							unit_product_no: dtl_item.unit_product_no,
							unit_name: dtl_item.unit_name,
							current_price: dtl_item.current_price,
							qty: dtl_item.select_number
						})
					}
				})
			})
			currentGood.combos = combos
		},

		// 取消调味选择
		minusSpec(condimentIndex, condimentGroupIndex) {
			let condimentGroup = this.currentGood.condiment_group[condimentGroupIndex]
			let condiment = condimentGroup.condiment
			condiment[condimentIndex].select = false
			condiment[condimentIndex].select_number = 0
			this.calcProductPrice()
		},
		// 取消规格选择
		delCombosDtl(combosDtlIndex, combosIndex) {
			let combosItem = this.currentGood.combos[combosIndex]
			let combosDtl = combosItem.dtl
			combosDtl[combosDtlIndex].select = false
			combosItem.select_number -= combosDtl[combosDtlIndex].select_number
			if (combosItem.select_number <= 0) {
				combosItem.select_number = 0
			}
			combosDtl[combosDtlIndex].select_number = 0
			this.calcProductPrice()
		},
		// 商品总价计算方式
		goodPriceCalculation(money, spec, good = undefined) {
			let currentGood = this.currentGood;
			if (good != undefined) {
				currentGood = good;
			}
			switch (spec.calculation) {
				case "plus":
					currentGood.currentGoodPrice += money;
					currentGood.currentGoodPlusPrice += money;
					break;
				case "replace":
					currentGood.currentGoodPrice = money;
					currentGood.unit = spec.name;
					break;
			}
		},
	},
};
</script>

<style lang="scss" scoped>
scroll-view ::-webkit-scrollbar {
	width: 0;
	height: 0;
	background-color: transparent;
}

.goods-list-box {
	height: 100%;
}

.no-more-info {
	padding: 20rpx 0;
	width: 100%;
	font-size: 28rpx;
	color: #333;

}

.current-good-spec-box {
	flex-direction: column;

	.title-box {
		padding: 40rpx;
		justify-content: baseline;
		@include bdrStyle(0, '', #F1F1F1);

		.left-img-box {
			display: flex;
			justify-content: center;
			align-items: center;
			line-height: 0;
			font-size: 0;

			.img-hook {
				height: 180rpx !important;
				width: 180rpx !important;
				border-radius: $uni-border-radius-min;
				;
			}
		}

		.info-content {
			justify-content: space-between;
			flex-direction: column;
			font-size: 30upx;
			flex: 1;
			box-sizing: border-box;
			padding-left: 20rpx;
			font-size: 36rpx;
			color: #333333;

			.goods-name {
				height: 100rpx;
				overflow: hidden;
				padding-right: 40rpx;
			}

			.select-text {
				font-size: 28rpx;
				color: #666666;
				max-height: 100rpx;
				overflow: hidden;
			}
		}

		.reset-btn {
			right: 40rpx;
			bottom: 40rpx;
			width: 124rpx;
			height: 124rpx;
			border-radius: $uni-border-radius-min;
			;
			background-color: #fff;
			border: 1rpx solid $u-primary;
			color: $u-primary;
			width: 120rpx;
			height: 60rpx;
			line-height: 58rpx;
			font-size: 28rpx;
		}
	}

	.goods-number {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 40rpx;
	}

	.good-spec-box {
		max-height: 800rpx;
		overflow-y: scroll;

		.group-title {
			padding: 20rpx 40rpx;
			font-size: 28rpx;
			color: #666666;
			padding-top: 0;
		}

		.check-num-count {
			right: -10rpx;
			top: -16rpx;
			color: #fff;
			background-color: $u-primary;
			border-radius: 50%;
			width: 36rpx;
			height: 36rpx;
			line-height: 36rpx;
			font-size: 26rpx;
		}

		.group-specs {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			box-sizing: border-box;
			padding: 0 40rpx;

			.spec {
				min-width: 86rpx;
				height: 58rpx;
				text-align: center;
				background: #EFEFEF;
				border-radius: 6rpx;
				font-size: 26rpx;
				color: #666666;
				margin-right: 20rpx;
				margin-bottom: 30rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				white-space: nowrap;
				border: 1rpx solid #ffffff;
				padding: 0 20rpx;
			}

			.select {
				color: $u-primary;
				border: 1rpx solid $u-primary;
				background: rgba(255, 96, 80, 0.10);
			}

			.condiment_box {
				position: relative;

				.close {
					right: 0rpx;
				}
			}


		}
	}

	.good-spec-bottom {
		justify-content: space-between;
		padding: 0 40rpx;
		height: 120rpx;
		align-items: center;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		@include bdrStyle('', 0, #F1F1F1);

		.red {
			color: $u-primary;
			font-size: 40rpx;
		}
	}

	.font-style {
		font-size: 28rpx;
		color: #666666;
	}


}

.sub-combos {
	font-size: 24rpx;
	color: #FA952F;
	margin-left: 10rpx;
}

.sub {
	font-size: 26upx;
	color: #666;
}

.car-more-msg {
	left: -100rpx;
	top: 8rpx;
	background: #EFEFEF;
	border-radius: 100px;
	font-size: 22rpx;
	color: #666666;
	width: 66rpx;
	height: 32rpx;
}

.good-bottom {
	font-size: 30upx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-sizing: border-box;

	.good-price {
		.red {
			color: $u-primary;
			display: flex;
			align-items: center;

			.goods-price-sale {
				font-size: 36rpx;
			}

			.u-tag {
				margin-left: 10rpx;
			}
		}

		.gray {
			color: #ccc;
			text-decoration: line-through;
			font-size: 26upx;
		}

		.spec-key {
			color: #999;
			font-size: 26upx;
		}
	}
}

.good-list {
	background-color: #fff;

	.list-title {
		position: sticky;
		height: 60rpx;
		background: #FAFAFA;
		z-index: 10;
		top: 0;
		font-size: 32rpx;
		color: #696969;
		display: flex;
		align-items: center;
		padding: 0 20rpx;
		box-sizing: border-box;

		.list-title-val {
			width: 100%;
			white-space: nowrap;
			overflow: hidden;
		}
	}

	.good-li {
		display: flex;
		align-items: center;
		padding: 32rpx 30rpx;
		padding-left: 0;
		margin-left: 20rpx;
		@include bdrStyle(0, '', #F1F1F1);
		position: relative;
		height: 144rpx;

		&:last-child {
			@include bdrStyle('', '', #ffffff);
		}

		.good-img {
			margin-right: 20rpx;
			position: relative;
			font-size: 0;
			line-height: 0;
			height: 100%;
			width: 144rpx !important;

			.img-obj {
				height: 144rpx !important;
				width: 144rpx !important;
				border-radius: $uni-border-radius-min;
			}

			.sell-out {
				position: absolute;
				left: 0;
				bottom: 0rpx;
				width: 160rpx;
				background-color: #000000;
				opacity: 0.5;
				color: white;
				text-align: center;
				font-size: 20rpx;
				z-index: 10;
				height: 30rpx;
				width: 100%;
				line-height: 30rpx;
			}
		}

		.good-num {
			display: flex;
			align-items: center;
			justify-content: center;
			position: absolute;
			top: 10rpx;
			left: 10rpx;
			color: #ffffff;
			background-color: #e9551f;
			height: 32rpx;
			width: 32rpx;
			font-size: 28rpx;
			border: 1rpx solid #e9551f;
			border-radius: 28rpx;
		}

		.flex-item {
			flex: 1;
			display: flex;
			flex-direction: column;
			height: 100%;
			justify-content: space-between;

			.special-offer {
				background-color: $u-primary;
				color: #ffffff;
				font-size: 22rpx;
				border-radius: $uni-border-radius-min;
				;
				padding: 0 8rpx;
				margin-left: 8rpx;
			}

			.old-price {
				color: #ccc;
				text-decoration: line-through;
				font-size: 28rpx;
				padding-left: 10rpx;
			}

			.good-name {
				width: 100%;
				font-size: 30rpx;
				color: #333333;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.product-name {
					flex: 0 0 220rpx;
					width: 220rpx;
					height: 84rpx;
					font-size: 32rpx;
					overflow: hidden;
				}

				.combos-key {
					color: #999;
					font-size: 26upx;
				}
			}

			.good-info {
				.good-price {
					float: left;
					font-size: 26upx;
					color: red;
				}
			}
		}

	}
}

.is-c-out {
	background-color: #fff;
	opacity: 0.3;
}


.plus {
	width: 40rpx;
	height: 40rpx;
	background-color: #e38c3a;
	border-radius: 50%;
	display: flex;
	justify-content: center;
	align-items: center;
}

.btn-spec {
	line-height: 32rpx;
	height: 84rpx;

	.goods-mark {
		height: 32rpx;
		border-radius: 99999rpx;
		border: 1px solid #FA952F;
		color: #FA952F;
		padding: 0rpx 10rpx;
		font-size: 22rpx;
	}
}
</style>